package it.xiuxian.lib.utils;

import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import it.xiuxian.lib.R;

public class ItemClickSupport {
    private final RecyclerView mRecyclerView;
    private ItemClickSupport.OnItemClickListener mOnItemClickListener;
    private ItemClickSupport.OnItemLongClickListener mOnItemLongClickListener;
    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        public void onClick(View v) {
            if (ItemClickSupport.this.mOnItemClickListener != null) {
                RecyclerView.ViewHolder holder = ItemClickSupport.this.mRecyclerView.getChildViewHolder(v);
                ItemClickSupport.this.mOnItemClickListener.onItemClicked(ItemClickSupport.this.mRecyclerView, holder.getAdapterPosition(), v);
            }

        }
    };
    private View.OnLongClickListener mOnLongClickListener = new View.OnLongClickListener() {
        public boolean onLongClick(View v) {
            if (ItemClickSupport.this.mOnItemLongClickListener != null) {
                RecyclerView.ViewHolder holder = ItemClickSupport.this.mRecyclerView.getChildViewHolder(v);
                return ItemClickSupport.this.mOnItemLongClickListener.onItemLongClicked(ItemClickSupport.this.mRecyclerView, holder.getAdapterPosition(), v);
            } else {
                return false;
            }
        }
    };
    private RecyclerView.OnChildAttachStateChangeListener mAttachListener = new RecyclerView.OnChildAttachStateChangeListener() {
        public void onChildViewAttachedToWindow(View view) {
            if (ItemClickSupport.this.mOnItemClickListener != null) {
                view.setOnClickListener(ItemClickSupport.this.mOnClickListener);
            }

            if (ItemClickSupport.this.mOnItemLongClickListener != null) {
                view.setOnLongClickListener(ItemClickSupport.this.mOnLongClickListener);
            }

        }

        public void onChildViewDetachedFromWindow(View view) {
        }
    };

    private ItemClickSupport(RecyclerView recyclerView) {
        this.mRecyclerView = recyclerView;
        this.mRecyclerView.setTag(R.id.frameLayoutLoading, this);
        this.mRecyclerView.addOnChildAttachStateChangeListener(this.mAttachListener);
    }

    public static ItemClickSupport addTo(RecyclerView view) {
        ItemClickSupport support = (ItemClickSupport)view.getTag(R.id.frameLayoutLoading);
        if (support == null) {
            support = new ItemClickSupport(view);
        }

        return support;
    }

    public static ItemClickSupport removeFrom(RecyclerView view) {
        ItemClickSupport support = (ItemClickSupport)view.getTag(R.id.frameLayoutLoading);
        if (support != null) {
            support.detach(view);
        }

        return support;
    }

    public ItemClickSupport setOnItemClickListener(ItemClickSupport.OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
        return this;
    }

    public ItemClickSupport setOnItemLongClickListener(ItemClickSupport.OnItemLongClickListener listener) {
        this.mOnItemLongClickListener = listener;
        return this;
    }

    private void detach(RecyclerView view) {
        view.removeOnChildAttachStateChangeListener(this.mAttachListener);
        view.setTag(R.id.frameLayoutLoading, (Object)null);
    }

    public interface OnItemLongClickListener {
        boolean onItemLongClicked(RecyclerView var1, int var2, View var3);
    }

    public interface OnItemClickListener {
        void onItemClicked(RecyclerView var1, int var2, View var3);
    }
}
